$(function () {
    var clz_datagrid = $("#clz_datagrid");
    var clz_dialog = $("#clz_dialog");
    var clz_form = $("#clz_form");
    clz_datagrid.datagrid({
        url: '/classRoom/query.do',
        fit: true,
        fitColumns: true,
        columns: [[
            {field: 'id', title: '班级编号', width: 100},
            {field: 'className', title: '班级名称', width: 100},
            {field: 'studentAmount', title: '学生数量', width: 100},
            {
                field: 'college', title: '所在学院', width: 100, formatter: function (value) {
                return value ? value.school : "";
            }
            },
            {
                field: 'classState', title: '班级状态', width: 100, formatter: function (value) {
                return value ? "已开班" : "<font color='red'>未开班</font>"
            }
            },
            {
                field: 'locationClass', title: '所在教室', width: 100, formatter: function (value) {
                return value ? value.name : ""
            }
            },
            {
                field: 'classHead', title: '班主任', width: 100, formatter: function (value) {
                return value ? value.username : ""
            }
            }
        ]],
        singleSelect: true,
        pagination: true,
        toolbar: "#clz_btns",
        onClickRow: function (index, row) {
            if (row.state) {
                $("#changeState_btn").linkbutton({
                    text: '已开班'
                })
            } else {
                $("#changeState_btn").linkbutton({
                    text: '未开班'
                })
            }
        }
    });
    clz_dialog.dialog({
        buttons: "#save_btns",
        closed: true,
        onClose: function () {
            clz_form.form('clear');
        }
    });
    var methodObj = {
        add: function () {
            clz_dialog.dialog('setTitle', '新增班级');
            clz_dialog.dialog('open');
        },

        edit: function () {
            var row = clz_datagrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert('温馨提示', "请选中一条数据", 'warning');
                return;
            }
            clz_form.form('load', row
            );
            clz_dialog.dialog('setTitle', '编辑班级');
            clz_dialog.dialog('open');
        },
        del: function () {
            var row = clz_datagrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert('温馨提示', "请选中一条数据", 'warning');
                return;
            }
            $.messager.confirm('确认对话框', '您确定要删除该班级吗？', function (r) {
                if (r) {
                    $.get("/classRoom/delete.do", {id: row.id}, function (data) {
                        if (data.success) {
                            $.messager.alert('温馨提示', '删除成功', 'info')
                            clz_datagrid.datagrid('reload');
                        } else {
                            $.messager.alert('温馨提示', data.msg, 'warning');
                        }
                    })
                }
            });
        },
        reload: function () {
            clz_datagrid.datagrid('reload');
        },

        save: function () {
            clz_form.form('submit', {
                url: '/classRoom/saveOrUpdate.do',
                success: function (data) {
                    data = $.parseJSON(data);
                    if (data.success) {
                        $.messager.alert('温馨提示', '保存成功', 'info')
                        methodObj.cancel();
                        clz_datagrid.datagrid('reload');
                    } else {
                        $.messager.alert('温馨提示', data.msg, 'error');
                    }
                }
            })
        },
        changeState: function () {
            var row = clz_datagrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert('温馨提示', "请选中一条数据", 'warning');
                return;
            }
            $.messager.confirm('确认对话框', '您确定执行该操作吗？', function (r) {
                if (r) {
                    $.get("/classRoom/changeState.do", {id: row.id}, function (data) {
                        if (data.success) {
                            $.messager.alert('温馨提示', '操作成功', 'info')
                            clz_datagrid.datagrid('reload');
                        } else {
                            $.messager.alert('温馨提示', data.msg, 'warning');
                        }
                    })
                }
            });
        },
        cancel: function () {
            clz_dialog.dialog('close');
        }
    }

    $("a[data-cmd]").click(function () {
        var methodName = $(this).data('cmd');
        methodObj[methodName]();
    })
})



